

IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[HC_ITEM_DETAIL_UPDATE]') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [dbo].[HC_ITEM_DETAIL_UPDATE]
GO

CREATE PROCEDURE [dbo].[HC_ITEM_DETAIL_UPDATE]
(
    @ITEM_ID int ,
    @ITEM_ADDRESS Nvarchar(500),
    @ENV_CD NVARCHAR(20),
    @USR_CRTN_ID VARCHAR(50),
	@ITEM_DETAIL_ID int  output
)
AS

BEGIN
	SELECT @ITEM_DETAIL_ID= ITEM_DETAIL_ID FROM DBO.HC_ITEM_DETAIL WHERE ITEM_ID=@ITEM_ID
	AND ENV_CD=@ENV_CD
	IF @ITEM_DETAIL_ID IS NULL
	BEGIN
	INSERT INTO dbo.HC_ITEM_DETAIL
           (
            ITEM_ID,
            ITEM_ADDRESS,
            ENV_CD,
            USR_CRTN_ID, 
            USR_MDFN_ID 
		)
     VALUES
      (
			@ITEM_ID,
			@ITEM_ADDRESS,
			@ENV_CD,
            @USR_CRTN_ID,
            @USR_CRTN_ID   
		)     
		SELECT @ITEM_DETAIL_ID = SCOPE_IDENTITY()
   END
   ELSE
   BEGIN
   UPDATE DBO.HC_ITEM_DETAIL SET ITEM_ADDRESS=@ITEM_ADDRESS
    WHERE ITEM_ID=@ITEM_ID
	AND ENV_CD=@ENV_CD
   END

END
GO
